package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Destination;

import cn.wolfcode.luowowo.query.DestinationQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 目的地管理列表
 */
@Controller
@RequestMapping("destination")
public class DestinationController {


    @Autowired
    private IDestinationService destinationService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DestinationQuery qo){

        // qo共享
        // page共享
        Page<Destination> page =  destinationService.query(qo);
        model.addAttribute("page",page);

        // 目的地页面(吐司) 中国>>黑龙江>>哈尔滨功能实现
         List<Destination> toasts = destinationService.queryToasts(qo.getParentId());
        // toasts共享
        model.addAttribute("toasts",toasts);
        return "destination/list";
    }

    // 添加按钮中保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination){
        destinationService.saveOrUpdate(destination);
        return JsonResult.success();

    }


}
